package com.lestar.htmlparser;

import com.lestar.htmlparser.jiuyou.I_JUParser;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Tag;
import org.htmlparser.util.DefaultParserFeedback;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserFeedback;
import org.htmlparser.util.SimpleNodeIterator;

import java.util.List;

/**
 * Created by Ray on 2016/10/11.
 */

public abstract class BaseParser<T> implements I_JUParser<T> {

    protected Node temp;

    public BaseParser() {
    }

    @Override
    public List<T> parseList(String source){
        return null;
    }

    @Override
    public T parseItem(String source){
        return null;
    }

    protected void findNode(NodeList nodeList, DeepFilter deepFilter) {
        //迭代开始
        if (deepFilter.isDeepMatch()) {
            NodeList extractNodes = nodeList.extractAllNodesThatMatch(deepFilter.getFilter(), true);
            if (extractNodes.size() != 0) {
                if (deepFilter.goDeeper()) {
                    findNode(extractNodes.elementAt(0).getChildren(), deepFilter);
                } else {
                    temp = extractNodes.elementAt(0);
                }
            }
        } else {
            NodeList extractNodes = nodeList.extractAllNodesThatMatch(deepFilter.getFilter(), false);
            if (extractNodes.size() != 0) {
                if (deepFilter.goDeeper()) {
                    findNode(extractNodes.elementAt(0).getChildren(), deepFilter);
                } else {
                    temp = extractNodes.elementAt(0);
                }
            }
        }
    }


    protected void deepSearch(NodeList nodeList, NodeFilter parentFilter, String keyword) {
        //迭代开始
        SimpleNodeIterator iterator = nodeList.elements();
        while (iterator.hasMoreNodes()) {
            Node node = iterator.nextNode();
            //得到该节点的子节点列表
            NodeList childList = node.getChildren();
            //孩子节点为空，说明是值节点
            if (null == childList) {
                //得到值节点的值
            } else {
                String attribute;
                if (node instanceof Tag) {
                    attribute = ((Tag) node).getAttribute("class");
                    if (attribute != null && attribute.contains(keyword)) {
                        if ("main-con".equals(keyword)) {
                            deepSearch(childList, parentFilter, "hope-rank-con");
                        }
                        if ("hope-rank-con".equals(keyword)) {
                            deepSearch(childList, parentFilter, "box-text");
                        }
                        if ("box-text".equals(keyword)) {
                            System.out.println(node.toString());
                        }
                        return;
                    }
                }
                childList = childList.extractAllNodesThatMatch(parentFilter);
                deepSearch(childList, parentFilter, keyword);
            }//end else
        }//end wile
    }

    protected ParserFeedback getFeedback() {
        return new DefaultParserFeedback();
    }

    protected static void message(String szMsg) {
        try {
            System.out.println(szMsg);
        } catch (Exception e) {
        }
    }
}
